﻿#region Librerias using System;using System.Collections.Generic;using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;#endregion

namespace ESF.WinForms.Controls.ImageControls
{
	public class uscImageSheetGroupSet : TableLayoutPanel
	{
		#region Miembros

		private frmEditImage _frmEditImage;

		#endregion


		#region Delegados		public delegate void ItemMouseDoubleClick (ref uscImageSheetItem pItem);		public event ItemMouseDoubleClick OnItemMouseDoubleClick;		#endregion						#region Propiedades
        public int SetCount { get { return this.Controls.Count; } }

		private uscImageSheetSet CurrentSet
		{
			get
			{
				if (Controls.Count == 0) { return null; }

				return ((Controls [Controls.Count - 1] as GroupBox).Controls [0]) as uscImageSheetSet;
			}
		}

		#endregion       
        #region Constructor		public uscImageSheetGroupSet ()        {			AutoScroll = true;            AutoSize = true;			_frmEditImage = new frmEditImage ();						Dock = DockStyle.Top;        }                #endregion                        #region Funciones        		public void AddSet (string pSetName)		{			GroupBox grpBox;			grpBox = new GroupBox ();			grpBox.Dock = DockStyle.Top;			grpBox.Text = pSetName;						grpBox.Height = uscImageSheetItem.HEIGHT + (uscImageSheetItem.PADDING * 2) + 24;						grpBox.Controls.Add (new uscImageSheetSet (pSetName));			grpBox.AllowDrop = true;			grpBox.DragEnter += delegate (object sender, DragEventArgs e)
			{
				if (e.Data.GetDataPresent(typeof(uscImageSheetItem))) 
				{
					e.Effect = DragDropEffects.Move;
				}
			};			grpBox.DragDrop += delegate (object sender, DragEventArgs e) 
			{
				if (e.Data.GetDataPresent(typeof(uscImageSheetItem))) 
				{
					uscImageSheetItem item = e.Data.GetData(typeof(uscImageSheetItem)) as uscImageSheetItem;
					
					if (MessageBox.Show (this, "Confirma que desea eliminar este elemento?", "Eliminar elemento", MessageBoxButtons.YesNo) == DialogResult.Yes)
					{
						item.Parent.Controls.Remove (item);
					}
				}			};						Controls.Add (grpBox);			Invalidate ();		}		public void AddImage (string pImageFilename)		{			uscImageSheetItem item;			item = new uscImageSheetItem ();			item.ImageFilename = pImageFilename;			item.DoubleClick += ItemMouseDoubleClick_EventHandler;			if (CurrentSet == null)			{				AddSet ("Default");			}			CurrentSet.Add (item);		}		public void AddImageSheetSet (uscImageSheetSet pImageSheetSet)		{			Controls.Add (pImageSheetSet);		}                #endregion


		#region Eventos

		protected void ItemMouseDoubleClick_EventHandler(object sender, EventArgs e)
		{
			uscImageSheetItem item;
			
			
			item = sender as uscImageSheetItem;

			_frmEditImage.ImageFilename = item.ImageFilename;
			_frmEditImage.ShowDialog (this);

			if (OnItemMouseDoubleClick != null)
			{
				OnItemMouseDoubleClick (ref item);
			}
		}

		#endregion
	}
}